
#include "iostream"
#include "vector"
#include "unordered_map"
#include "unordered_set"
#include "algorithm"
#include "string"
#include "list"
#include "string.h"

class Solution {
public:
    int index2(char a) {
        if (a >= '0' && a <= '9') {
            return a - '0';
        }
        switch (a) {
            case 'A':
                return 14;
            case 'K':
                return 13;
            case 'Q':
                return 12;
            case 'J':
                return 11;
            case 'T':
                return 10;
        }
    }

    int index1(char a) {
        switch (a) {
            case 'S':
                return 0;
            case 'H':
                return 1;
            case 'D':
                return 2;
            case 'C':
                return 3;
        }
    }

    void sort(std::vector<std::string> &ps) {
        for (int i = 0; i < ps.size(); i++) {
            for (int j = 0; j < ps.size() - i - 1; j++) {
                if(index2(ps[j][1])<index2(ps[j+1][1])){
                    std::swap(ps[j],ps[j+1]);
                }else if(
                        index2(ps[j][1])==index2(ps[j+1][1])
                        ){
                    if(index1(ps[j][0])>index1(ps[j+1][0])){
                        std::swap(ps[j],ps[j+1]);
                    }
                }
            }
        }
    }

    void oneline() {
        std::vector<std::string> ps;
        for (int i = 0; i < 13; i++) {
            std::string s;
            char a = std::cin.get();
            char b = std::cin.get();
            s += a;
            s += b;
            ps.push_back(s);
            char c = std::cin.get();
        }
        sort(ps);
        bool first= true;
        for(auto &s:ps){
            if(!first){
                printf(" ");
            }
            first=false;
            std::cout<<s;
        }
        printf("\n");
    }

    void run() {

        bool first = true;
        int a;
        while (std::cin >> a) {
            while ((char) std::cin.get() != '\n');
            while (a) {

                for (int i = 0; i < 4; i++) {
                    oneline();
                }
                printf("\n");
                a--;
            }
        }
    }
};

int main() {
//    bool cur_num= true;
    Solution().run();
    return 0;
}

